package com.cuckoo.cuckoo_app2.business.Imp;

import com.cuckoo.cuckoo_app2.business.PlatListBusiness;
import com.cuckoo.cuckoo_app2.business.PlatformBusiness;
import com.cuckoo.cuckoo_app2.business.SiteLinkBusiness;
import com.cuckoo.cuckoo_app2.dao.PlatList;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.SiteLink;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlatListBusinessImp implements PlatListBusiness {

    @Override
    public List<PlatList> getPlatList() {
        Map<String, PlatList> map = new HashMap<>();

        PlatformBusiness platform = new PlatformBusinessImp();
        List<Platform> platforms = platform.getAll();

        SiteLinkBusiness siteLink = new SiteLinkBusinessImp();
        List<SiteLink> siteLinks = siteLink.getAll();

        // 将已添加的数据放入 map
        PlatList platList;
        for (Platform pla : platforms) {
            platList = new PlatList();
            platList.setId(pla.getId());
            platList.setImageUrl(pla.getImageUrl());
            platList.setTitle(pla.getTitle());
            platList.setStart(1);
            map.put(pla.getId(), platList);
        }

        // 再循环平台为了保持排序
        String id;
        List<PlatList> list = new ArrayList<>();
        for (SiteLink sit : siteLinks) {
            platList = new PlatList();
            id = sit.getId();
            platList.setId(sit.getId());
            platList.setImageUrl(sit.getImageUrl());
            platList.setTitle(sit.getTitle());
            platList.setStart(0);
            // 判断是否已添加
            if (map.getOrDefault(id, null) != null) {
                platList = map.get(sit.getId());
                platList.setStart(1);
                map.remove(id);
            }
            list.add(platList);
        }

        // 最后导出map 加入到列表中
        for (PlatList plat : map.values()) {
            list.add(plat);
        }

        return list;
    }
}
